package day06;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * 编写一个程序，将当前目录下所有的员工文件进行读取，并解析出所有员工为Emp
 * 对象并存入Map。其中key为该员工的名字，value为该员工的emp对象。
 * 然后，要求用户输入一个员工名字，若该员工存在，则输出该员工的名字，年龄，工资，以及入职20周年的纪念日当周的周六的日期。
 * 即:输入名字"张三"
 * 若该员工存在，则输出如下格式:
 * 张三,25,5000,2006-02-14
 * 入职20周年纪念日派对日期: 2026-02-14  （注:若入职日期为:2006-02-14）
 * 若该员工不存在，则输出:"查无此人"
 * @author Bonnie
 *
 */
public class Test12 {
    static Calendar cal = Calendar.getInstance();
    public static void main(String[] args) {
        Map<String,Emp> empMap = new HashMap<>();
        getEmpMap(empMap);
        System.out.println("输入员工姓名：");
        Scanner input = new Scanner(System.in);
        String name = input.next();
        if (empMap.containsKey(name)){
            empOut(empMap.get(name));
        }else {
            System.out.println("查无此人");
        }

    }

    private static void getEmpMap(Map<String,Emp> map){
        File currentDir = new File("Home_Work/src/day06/");
        File[] files = currentDir.listFiles((dir, name) -> name.startsWith("emp张"));
        if (files != null) {
            for (File file : files) {
                try {
                    Emp e = getEmp(file);
                    map.put(e.getName(), e);
                } catch (IOException | ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private static Emp getEmp(File fname) throws IOException, ParseException {
        FileReader fr = new FileReader(fname);
        BufferedReader read= new BufferedReader(fr);
        String str = null;
        try {
            str = read.readLine();
        }catch (IOException e){
            e.printStackTrace();
        }
        String[] strs = str.split(",");
        String name = strs[0];
        int age = Integer.parseInt(strs[1]);
        String gender = strs[2];
        int salay = Integer.parseInt(strs[3]);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(strs[4]);
        fr.close();
        read.close();
        return new Emp(name,age,gender,salay,date);
    }

    private static void empOut(Emp e){
        List<String> strs = Arrays.asList(e.toString().split(","));
        strs = new ArrayList<>(strs);
        strs.remove(2);
        String inf = strs.toString();
        LocalDate MemorialDay = LocalDate.parse(strs.get(3), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        MemorialDay = MemorialDay.plusYears(20);
        MemorialDay = MemorialDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
        System.out.println(inf.substring(1,inf.length()-1));
        System.out.println("入职20周年纪念日派对日期:"+MemorialDay);

//        cal.setTime(e.getHiredate());
//        cal.add(Calendar.YEAR, 20);
//        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
//        System.out.println(cal.getTime());
    }

}
